--- title: complexnet keywords: fastai sidebar: home_sidebar summary: "CNN whose topology is built by complex networks." ---
{% raw %}
import tensorflow_datasets as tfds
cifar10, info = tfds.load(name='food101', with_info=True)
Downloading and preparing dataset food101/2.0.0 (download: 4.65 GiB, generated: Unknown size, total: 4.65 GiB) to /home/fengwf/tensorflow_datasets/food101/2.0.0...
/home/fengwf/anaconda3/lib/python3.7/site-packages/urllib3/connectionpool.py:847: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
import matplotlib.pyplot as plt
tfds.show_examples(info, cifar10['train'])

xception_block[source]

xception_block(inputs, filters, kernel_size, strides)

complex_net[source]

complex_net(G, input, filters, kernel_size, stages_nums:tuple)

input = keras.Input(shape=(32,32,3))
n = 60
stages_nums = [n//3]*3
G = cascade(n, 0.1)
G = after_DAG(G)
for _id in sorted(G.nodes()):
    preds = [pred for pred in G.predecessors(_id)]
    succs = [succ for succ in G.successors(_id)]
    print(_id,preds, succs)
-1 [] [0, 1, 2, 3, 7, 8, 10]
0 [-1] [16, 18, 20, 21, 24, 29, 30, 35, 40, 43, 48, 54, 57, 58, 59]
1 [-1] [14, 15, 18, 20, 32, 33, 38, 47, 48, 51, 53, 55]
2 [-1] [4, 13, 16, 27, 29, 38, 39, 42, 44, 49]
3 [-1] [5, 6, 30, 35, 37, 39, 41, 42, 54]
4 [2] [9, 18, 19, 25, 32, 34, 39, 42, 43, 48, 49, 56, 59]
5 [3] [6, 11, 18, 22, 23, 24, 26, 31, 35, 36, 38, 43, 52, 55, 57]
6 [3, 5] [12, 17, 18, 22, 25, 31, 42, 46, 49, 52, 55]
7 [-1] [11, 18, 19, 20, 21, 27, 37, 49]
8 [-1] [12, 13, 15, 18, 24, 25, 27, 35, 43, 49, 51, 55]
9 [4] [17, 19, 25, 26, 33, 35, 38, 41, 42, 46, 47, 51, 55]
10 [-1] [11, 16, 28, 34, 37, 41, 42, 45]
11 [5, 7, 10] [14, 25, 28, 40, 41, 48, 55, 58]
12 [6, 8] [18, 19, 32, 33, 34, 35, 36, 41, 43, 54, 57, 59]
13 [2, 8] [14, 15, 17, 27, 29, 31, 34, 41, 48, 55, 59]
14 [1, 11, 13] [17, 18, 44, 48, 55, 57]
15 [1, 8, 13] [17, 21, 28, 33, 37, 38, 49, 50, 56]
16 [0, 2, 10] [19, 23, 29, 31, 36, 42, 43, 46, 58]
17 [6, 9, 13, 14, 15] [18, 25, 29, 31, 36, 41, 42, 44, 50, 58]
18 [0, 1, 4, 5, 6, 7, 8, 12, 14, 17] [23, 24, 29, 34, 40, 46, 47, 52, 54, 55]
19 [4, 7, 9, 12, 16] [22, 25, 35, 43, 46]
20 [0, 1, 7] [27, 30, 31, 38, 47, 48, 52, 53, 59]
21 [0, 7, 15] [23, 24, 35, 38, 52, 54]
22 [5, 6, 19] [24, 31, 32, 33, 34, 44, 53, 55]
23 [5, 16, 18, 21] [28, 30, 35, 39, 43, 47, 48, 50, 56, 57, 58]
24 [0, 5, 8, 18, 21, 22] [26, 29, 33, 41, 43, 44, 47, 52, 56, 59]
25 [4, 6, 8, 9, 11, 17, 19] [32, 33, 42, 49, 50, 52, 56]
26 [5, 9, 24] [28, 30, 32, 34, 41, 48, 49, 51]
27 [2, 7, 8, 13, 20] [35, 50, 53, 54, 58]
28 [10, 11, 15, 23, 26] [29, 34, 49, 54, 59]
29 [0, 2, 13, 16, 17, 18, 24, 28] [36, 37, 38, 42, 45, 49, 50, 55]
30 [0, 3, 20, 23, 26] [31, 37, 40, 42, 43, 55]
31 [5, 6, 13, 16, 17, 20, 22, 30] [32, 57, 58]
32 [1, 4, 12, 22, 25, 26, 31] [33, 34, 43, 50, 53, 55, 58]
33 [1, 9, 12, 15, 22, 24, 25, 32] [35, 37, 40, 44, 57]
34 [4, 10, 12, 13, 18, 22, 26, 28, 32] [36, 39, 41, 46, 50, 57]
35 [0, 3, 5, 8, 9, 12, 19, 21, 23, 27, 33] [36, 42, 44, 45, 47, 53, 54, 55, 57, 58]
36 [5, 12, 16, 17, 29, 34, 35] [45, 51, 57]
37 [3, 7, 10, 15, 29, 30, 33] [38, 40, 58]
38 [1, 2, 5, 9, 15, 20, 21, 29, 37] [39, 49, 51, 54, 58, 59]
39 [2, 3, 4, 23, 34, 38] [44, 45, 49, 51, 52]
40 [0, 11, 18, 30, 33, 37] [44, 53, 54, 58]
41 [3, 9, 10, 11, 12, 13, 17, 24, 26, 34] [42, 47, 53]
42 [2, 3, 4, 6, 9, 10, 16, 17, 25, 29, 30, 35, 41] [50, 55, 56]
43 [0, 4, 5, 8, 12, 16, 19, 23, 24, 30, 32] [54]
44 [2, 14, 17, 22, 24, 33, 35, 39, 40] [48, 54, 55, 56, 57, 58]
45 [10, 29, 35, 36, 39] [49, 54]
46 [6, 9, 16, 18, 19, 34] [58]
47 [1, 9, 18, 20, 23, 24, 35, 41] [50, 52]
48 [0, 1, 4, 11, 13, 14, 20, 23, 26, 44] [56, 57, 59]
49 [2, 4, 6, 7, 8, 15, 25, 26, 28, 29, 38, 39, 45] [51, 52, 57]
50 [15, 17, 23, 25, 27, 29, 32, 34, 42, 47] [56]
51 [1, 8, 9, 26, 36, 38, 39, 49] [56]
52 [5, 6, 18, 20, 21, 24, 25, 39, 47, 49] [55]
53 [1, 20, 22, 27, 32, 35, 40, 41] [56, 57, 58]
54 [0, 3, 12, 18, 21, 27, 28, 35, 38, 40, 43, 44, 45] [55]
55 [1, 5, 6, 8, 9, 11, 13, 14, 18, 22, 29, 30, 32, 35, 42, 44, 52, 54] [57]
56 [4, 15, 23, 24, 25, 42, 44, 48, 50, 51, 53] [60]
57 [0, 5, 12, 14, 23, 31, 33, 34, 35, 36, 44, 48, 49, 53, 55] [60]
58 [0, 11, 16, 17, 23, 27, 31, 32, 35, 37, 38, 40, 44, 46, 53] [60]
59 [0, 4, 12, 13, 20, 24, 28, 38, 48] [60]
60 [56, 57, 58, 59] []
model = complex_net(G, input, filters=32, kernel_size=3, stages_nums=stages_nums)
model.summary()
Model: "model_87"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_62 (InputLayer)        [(None, 32, 32, 3)]       0         
_________________________________________________________________
stem_conv (SeparableConv2D)  (None, 32, 32, 32)        155       
_________________________________________________________________
batch_normalization_2014 (Ba (None, 32, 32, 32)        128       
_________________________________________________________________
model_86 (Model)             (None, 8, 8, 64)          584352    
_________________________________________________________________
global_max_pooling2d_13 (Glo (None, 64)                0         
_________________________________________________________________
dense_11 (Dense)             (None, 10)                650       
=================================================================
Total params: 585,285
Trainable params: 569,861
Non-trainable params: 15,424
_________________________________________________________________
keras.utils.plot_model(model,'first_dag.png', show_layer_names=False)

complexnet_1[source]

complexnet_1(n, c, stages_nums:tuple, input, filters, kernel_size, num_classes)

n = 60*2
c = 0.1
stages_nums = [n//3]*3
filters = 64
kernel_size = 3
model = complexnet_1(n, c, stages_nums, input, filters, kernel_size, num_classes=10)

complex_stage[source]

complex_stage(G, input, filters, kernel_size, strides)

model =  complex_stage(G, input, filters=32, kernel_size=3, strides=2)

complexnet_2[source]

complexnet_2(n, c, input, filters, kernel_size, num_classes)

n = 20
c = 0.1
filters = 64
kernel_size = 3
num_classes = 10
model = complexnet_2(n, c, input, filters, kernel_size, num_classes)
model.summary()
Model: "model_76"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_55 (InputLayer)        [(None, 32, 32, 3)]       0         
_________________________________________________________________
stem_conv (SeparableConv2D)  (None, 32, 32, 32)        155       
_________________________________________________________________
batch_normalization_1553 (Ba (None, 32, 32, 32)        128       
_________________________________________________________________
model_73 (Model)             (None, 16, 16, 64)        146016    
_________________________________________________________________
model_74 (Model)             (None, 8, 8, 128)         533696    
_________________________________________________________________
model_75 (Model)             (None, 4, 4, 256)         2067968   
_________________________________________________________________
global_max_pooling2d_8 (Glob (None, 256)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 10)                2570      
=================================================================
Total params: 2,750,533
Trainable params: 2,721,797
Non-trainable params: 28,736
_________________________________________________________________
model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-3),
             loss=keras.losses.CategoricalCrossentropy(from_logits=False),
             metrics=['accuracy'])
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
model.fit(x_train, y_train,
          batch_size=64,
          epochs=1,
          validation_split=0.2)
Train on 40000 samples, validate on 10000 samples
40000/40000 [==============================] - 311s 8ms/sample - loss: 2.3041 - accuracy: 0.0983 - val_loss: 2.3040 - val_accuracy: 0.0980
<tensorflow.python.keras.callbacks.History at 0x7fc1dbe8bef0>
model.outputs
[<tf.Tensor 'dense_2/Identity:0' shape=(None, 10) dtype=float32>]
m.get_layer('stem_conv')
<tensorflow.python.keras.layers.convolutional.SeparableConv2D at 0x7fc487b4fe80>
{% endraw %}